#include <REGX52.H>
#include "Delay1ms.h"
#include "LCD1602.h"
#include "MatrixKey.h"
#include "INTRINS.h"
#include "string.h"


sbit pwm = P0^1 ;

//全局变量
unsigned char getkeyNum;
int count = 0;
unsigned char angle;	//旋转角度
char pw[6];
char pnum = 0;
char i = 0;
//char temp;


void Timer0Init(){
	EA = 1;		   //开启总中断
	ET0 = 1;	   //允许T0中断
	TMOD = 0x01;	//定时器模式，T0使用工作方式1
	TH0 = 0xFE;    //该初值使每0.5ms产生一次中断
	TL0 = 0x33;
	TR0 = 1;			 //打开T0定时器
} 


void Timer0() interrupt 1  {
	TH0 = 0xFE;    
	TL0 = 0x33;
	count++;
	if(count <= angle){
		pwm = 1;
	}
	else{
		pwm = 0;
	}
	
	if(count >= 40){	//形成20ms的脉冲
		count = 0;
	}		
}
//@11.0592MHz
void Delay3s(){	
	int j = 0;
	for(j;j<10;j++){
		unsigned char i, j, k;

		_nop_();
		_nop_();
		i = 5;
		j = 52;
		k = 195;
		do
		{
			do
			{
				while (--k);
			} while (--j);
		} while (--i);
	}
	
}


int main(){	
	Timer0Init();
	LCD_Init();

    while(1){
        getkeyNum = MatrixKey();
		//如果有按键输入
        if(getkeyNum != 100){
			//输出按键值
			if(getkeyNum < 10 )
				LCD_ShowChar(1,5,getkeyNum+48);
			else
				LCD_ShowChar(1,5,getkeyNum);
			
			pw[i++] = getkeyNum;
			pnum ++;
//			temp = pnum + 48;
//			LCD_ShowChar(1,3,temp);
			
			if(getkeyNum == 'D'){
				//如果存入值为6个且和密码相等，操作舵机
				if(pw[0]==6&& pw[1]==1&& pw[2]==6&& pw[3]==6&& pw[4]==6 && pw[5]==6){
                    count = 0;
					angle = 3;
					Delay3s();
					count = 0;
					angle = 1;
					LCD_ShowChar(1,1,'Y');
					
				}
				//如果密码不符合，重置pw
				else{
					LCD_ShowChar(1,1,'N');
				}
				pnum = 0;
				i = 0;
			}
			
			Delay1ms(500);
		}
		
	}
    return 0;
}












